using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HermesPortal.Core;  

public partial class Saga_Realms : PageBase
{
    private const  string GETPARENTREALMS ="1";
    private const string GETALLREALMS = "2";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            UpdateDataSource(GETPARENTREALMS);
        RealmList.RowDataBound += new GridViewRowEventHandler(RealmList_RowDataBound); 
    }

    protected void OnSelectedIndexChange_ddlGetRealmsView(object source, EventArgs e)
   {
       
       UpdateDataSource(ddlGetRealmsView.SelectedValue); 
   }

    private void UpdateDataSource(string viewStyle)
    {
        RealmList.Visible = true;
        RealmsTreeView1.Visible = false;
        switch (viewStyle)
        {
            // get only the parent nodes
            case "1":
                RealmDataSource.SelectParameters.Clear();
                RealmDataSource.SelectParameters.Add("SagaID", this.SagaID.ToString());
                RealmDataSource.SelectParameters.Add("RealmID", "0");
                // if its an admin get all realms for a saga
                if (Roles.IsUserInRole(User.Identity.Name, "sysAdmin"))
                {
                    RealmDataSource.SelectMethod = "AdminGetRealms";
                }
                else
                    RealmDataSource.SelectParameters.Add("CreatorID", this.Creator.CreatorID.ToString());
                break;
            // get all the nodes
            case "2":
                RealmDataSource.SelectParameters.Clear();
                RealmDataSource.SelectParameters.Add("SagaID", this.SagaID.ToString());
                // if its an admin get all realms for a saga
                if (Roles.IsUserInRole(User.Identity.Name, "sysAdmin"))
                {
                    RealmDataSource.SelectMethod = "AdminGetRealms";
                }
                else
                RealmDataSource.SelectParameters.Add("CreatorID", this.Creator.CreatorID.ToString() );
                break;
            // Load Tree View
            case "3":
                RealmList.Visible = false;
                RealmsTreeView1.Visible = true; 
                break;
        }
        lnkAddNewRealm.Visible = false;
        SagaRoles role = UserServices.GetCreatorRoleForSaga(Creator.Id, SagaID);
        if (role == SagaRoles.StoryGuide || User.IsInRole("sysAdmin"))
        {
            lnkAddNewRealm.Visible = true;
            lnkAddNewRealm.NavigateUrl = String.Format(@"~/Realms/Realm.aspx?SagaID={0}&RealmID={1}", SagaID, 0);
        }


    
    }

     protected void RealmList_RowDataBound( object source ,GridViewRowEventArgs e)
    {
       
        if (e.Row.RowIndex > 0)
        {
            int realmID = Convert.ToInt32(RealmList.DataKeys[e.Row.RowIndex].Value );
            DataList listSubNodes = (DataList)e.Row.FindControl("listSubNodes");
            Label lblSubRealms = (Label)e.Row.FindControl("lblSubRealms");
            StringDictionary realms = RealmServices.GetRealmSubNodesInfo(this.SagaID, realmID, ((PageBase)this.Page).Creator.Id);
            lblSubRealms.Visible = (realms.Count > 0);
            listSubNodes.Visible = (realms.Count > 0);
            if (realms.Count > 0)
            {
                listSubNodes.DataSource = realms;
                listSubNodes.DataBind();
            }
        }
         
    }

   
  
}
